package com.wjr.braveheart.http.oldversion;

import android.content.Context;
import android.text.TextUtils;

import com.wjr.braveheart.http.callback.EngineCallback;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by adminWJR on 2018/1/4.
 * 网络请求相关路径、apk、参数都需要放在JNI中，防止被反编译
 */

public class HttpUtils {

    private String mUrl;
    //请求方式
    private int mType = GET_TYPE;
    private static final int POST_TYPE = 0x0011;
    private static final int GET_TYPE = 0x0012;
    //默认引擎
    private static IHttpEngine mHttpEngine;
    private Map<String, Object> mParams;
    private Context mContext;

    private HttpUtils(Context context) {
        this.mContext = context;
        mParams = new HashMap<>();
    }

    /**
     * 在Application中初始化引擎
     *
     * @param httpEngine
     */
    public static void initHttpEngine(IHttpEngine httpEngine) {
        mHttpEngine = httpEngine;
    }

    /**
     * 创建 HttpUtils 实例
     *
     * @param context 用于取消网络请求的标记，一般传入Activity
     * @return
     */
    public static HttpUtils with(Context context) {
        return new HttpUtils(context);
    }

    /**
     * 设置 url
     *
     * @param url
     * @return
     */
    public HttpUtils url(String url) {
        this.mUrl = url;
        return this;
    }

    /**
     * Post 请求
     *
     * @return
     */
    public HttpUtils post() {
        mType = POST_TYPE;
        return this;
    }

    /**
     * Get 请求
     *
     * @return
     */
    public HttpUtils get() {
        mType = GET_TYPE;
        return this;
    }


    /**
     * 添加参数
     *
     * @param key
     * @param value
     * @return
     */
    public HttpUtils addParam(String key, Object value) {
        mParams.put(key, value);
        return this;
    }

    /**
     * 批量添加参数
     *
     * @param params
     * @return
     */
    public HttpUtils addParams(Map<String, Object> params) {
        mParams.putAll(params);
        return this;
    }

    //添加回调
    public void execute(EngineCallback callback) {

        if (callback == null) {
            callback = EngineCallback.DEFAULT_CALL_BACK;
        }
        onPrepare();
        callback.onPreExecute(mContext, mParams);

        //判断执行方法
        if (mType == POST_TYPE) {
            mHttpEngine.post(mContext, mUrl, mParams, callback);
        } else if (mType == GET_TYPE) {
            mHttpEngine.get(mContext, mUrl, mParams, callback);
        }
    }

    /**
     * 执行网络请求
     */
    public void execute() {
        execute(null);
    }

    /**
     * 取消网络请求
     */
    public void cancel() {
        mHttpEngine.cancel(mContext);
    }


    /**
     * 切换网络请求引擎
     *
     * @param httpEngine
     */
    public HttpUtils exchangeEngine(IHttpEngine httpEngine) {
        mHttpEngine = httpEngine;
        return this;
    }

    /**
     * 初始化加入一些参数和网络引擎
     */
    private void onPrepare() {
        if (TextUtils.isEmpty(mUrl)) {
            throw new NullPointerException("url 为 null！");
        }

        if (this.mHttpEngine == null) {
            throw new NullPointerException("第三方的引擎为空，请在Application中初始化！");
        }
    }

}
